package com.github.tetrisgame.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.github.tetrisgame.R;
import com.github.tetrisgame.databinding.ActivityVerifyBinding;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class VerifyActivity extends AppCompatActivity {
    ActivityVerifyBinding verifyBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        verifyBinding = ActivityVerifyBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(verifyBinding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        Intent intent = getIntent();
        String phone = intent.getStringExtra("phone");

        verifyBinding.verifyInputView.setOnInputListener(code -> {
            BmobSMS.verifySmsCode(
                    phone, code,
                    new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                //验证成功
                                //显式跳转：应用内部界面之间跳转
                                //隐式跳转：应用之间跳转 不知道跳转过去的界面的类
                                //Intent 意图对象
                                Intent intent = new Intent(VerifyActivity.this, MainActivity.class);
                                startActivity(intent);
                            } else {
                                Toast.makeText(VerifyActivity.this, "验证码不正确", Toast.LENGTH_LONG).show();
                                verifyBinding.verifyInputView.postDelayed(() -> {
                                    verifyBinding.verifyInputView.clear();
                                }, 1000);
                            }
                        }
                    });
        });

    }
}